﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.IO;
using System.Web.Hosting;

namespace WPFInterface.ViewModel
{
    public class AddUserViewModel : BaseViewModel
    {
        private List<string> _roleList;
        private bool _closeSignal;
        private ICommand _addCommand;
        private string _name;
        private string _firstname;
        private string _role;
        private string _password;
        private string _login;
        private HomeViewModel hvm;

        public HomeViewModel Hvm
        {
            get { return hvm; }
            set { hvm = value; }
        }

        public string Login
        {
            get { return _login; }
            set { _login = value; }
        }

        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }

        public string Role
        {
            get { return _role; }
            set { _role = value; }
        }

        public string Firstname
        {
            get { return _firstname; }
            set { _firstname = value; }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public List<string> RoleList
        {
            get { return _roleList; }
            set { _roleList = value; }
        }

        /// <summary>
        /// indique si on doit fermer la fenêtre ou non
        /// </summary>
        public bool CloseSignal
        {
            get { return _closeSignal; }
            set
            {
                if (_closeSignal != value)
                {
                    _closeSignal = value;
                    OnPropertyChanged("CloseSignal");
                }
            }
        }

        /// <summary>
        /// command pour s'authentifier
        /// </summary>
        public ICommand AddCommand
        {
            get { return _addCommand; }
            set { _addCommand = value; }
        }

        public AddUserViewModel()
        {
            RoleList = new List<string>();
            RoleList.Add("Chirurgien");
            RoleList.Add("Infirmière");
            RoleList.Add("Medecin");
            RoleList.Add("Radiologue");
            Role = RoleList[0];
            _addCommand = new RelayCommand(param => AddAccess(), param => true);
        }

        private void AddAccess()
        {
            ServiceUser.ServiceUserClient newuser = new ServiceUser.ServiceUserClient();
            Dbo.User user = new Dbo.User();
            user.Name = Name;
            user.Login = Login;
            user.Pwd = Password;
            user.Firstname = Firstname;
            user.Connected = false;
            user.Role = Role;
            /*
            if (Hvm.AvailablePictures.ContainsKey(Role))
                user.Picture = Hvm.AvailablePictures[Role];
            */
            try
            {
                newuser.AddUser(user);
                Hvm.ListUsers.Add(user);
            }
            catch (Exception e)
            {
                System.Windows.MessageBox.Show("The image is too big to upload!");
                //User could not be added...the image must be too large
                
            }
            this.CloseSignal = true;
        }
    }
}
